package cn.edu.just.firstjavaapp.l15;

import java.io.*;

public class ReadInvoice {

    static final String dataFile = "c:/java/invoicedata";
    public static void main(String[] args) throws IOException {

        DataInputStream in = null;
        double total = 0.0;
        try {
            in = new DataInputStream(new
                    BufferedInputStream(new FileInputStream(dataFile)));

            double price;
            int unit;
            String desc;

            try {
                while (true) {
                    price = in.readDouble();
                    unit = in.readInt();
                    desc = in.readUTF();
                    System.out.format("You ordered %d units of %s at $%.2f%n",
                            unit, desc, price);
                    total += unit * price;
                }
            } catch (EOFException e) { } catch (IOException e) {
                throw new RuntimeException(e);
            }
            System.out.format("For a TOTAL of: $%.2f%n", total);
        }
        finally {
            in.close();
        }
    }
}
